You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

28 lines
1.0 KiB

import { oauthManager } from '#server/service/oauth/oauth-manager';
import { setSessionCookie } from '#server/service/auth/cookie';
import { OAuthError } from '#server/service/oauth/oauth-error';
import { FRONTEND_LOGIN_PATH } from "common/config"
export default defineWrappedResponseHandler(async (event) => {
const providerName = getRouterParam(event, 'provider');
const query = getQuery(event);
const { code, state } = query as { code?: string; state?: string };
if (!code || !state) {
return sendRedirect(event, `${FRONTEND_LOGIN_PATH}?oauth_error=missing_params`);
}
try {
const result = await oauthManager.handleCallback(providerName!, code, state);
if (result.sessionId) {
setSessionCookie(event, result.sessionId);
}
return sendRedirect(event, `${FRONTEND_LOGIN_PATH}??oauth_success=1`);
} catch (error) {
const errorCode = error instanceof OAuthError ? error.code : 'OAUTH_UNKNOWN';
return sendRedirect(event, `${FRONTEND_LOGIN_PATH}??oauth_error=${errorCode}`);
}
});